package com.gwtgeo.ui.client.defbuilder;

import java.util.Map;

import org.gwtopenmaps.openlayers.client.layer.WMS;
import org.gwtopenmaps.openlayers.client.layer.WMSParams;

import com.gwtgeo.service.model.WMSLayerDef;

public class WMSLayerDefBuilder implements DefBuilder<WMSLayerDef, WMS>{

	public static final String WMS_PARAMS = "WMS_PARAMS";
	public static final String IS_TRANSPARENT = "IS_TRANSPARENT";
	public static final String URL = "URL";

	public WMSLayerDef buildDef(WMS layer, Map<String, Object> params) {

		WMSParams wmsParams = (WMSParams) params.get(WMS_PARAMS);
		Boolean isTransparent = (Boolean) params.get(IS_TRANSPARENT);
		String url = (String) params.get(URL);

		WMSLayerDef layerDef = new WMSLayerDef();
		layerDef.setFormat(wmsParams.getFormat());
		layerDef.setId(layer.getId());
		layerDef.setIsBaseLayer(layer.isBaseLayer());
		layerDef.setIsTransparent(isTransparent.booleanValue());
		layerDef.setIsVisble(layer.isVisible());
		layerDef.setLayers(wmsParams.getLayers());
		layerDef.setName(layer.getName());
		layerDef.setUrl(url);

		return layerDef;
	}

	@Override
	public WMS buildObj(WMSLayerDef def, Map<String, Object> params) {
		WMSParams wmsParams = new WMSParams();
		wmsParams.setFormat(def.getFormat());
		wmsParams.setIsTransparent(def.getIsTransparent());
		wmsParams.setLayers(def.getLayers());

		return new WMS(def.getName(), def.getUrl(), wmsParams);
	}
}
